package cn.t.web.android.config;

import cn.t.facade.android.engine.CabinetHandlingStrategy;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


/**
 * @author l
 */
@Component
public class DeviceTypeHandlerFactory implements InitializingBean, ApplicationContextAware {

    private final Map<String, CabinetHandlingStrategy> DEVICE_TYPE_MAP = new HashMap<>();

    private ApplicationContext appContext;

    public CabinetHandlingStrategy getHandler(String type) {
        return DEVICE_TYPE_MAP.get(type);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        appContext.getBeansOfType(CabinetHandlingStrategy.class)
                .values()
                .forEach(handler -> DEVICE_TYPE_MAP.put(handler.getDeviceProcessType(), handler));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        appContext = applicationContext;
    }
}
